#!/bin/csh -f
set echo_style=both
#PATHS
set SRCDIR=$DECAHOME"/SRC"
set EXEC=$DECAHOME"/bin/mkdeco_"
set PHELP="awk -f $SRCDIR/pophelp.awk $SRCDIR/mkdeco.help"
set PRAGHELP="awk -f $SRCDIR/pophelp.awk $SRCDIR/deco_pragmas.help"
set LSOPT="$DECAHOME/scripts/lsopt mkdeco"

#DEFAULTS

set NUOFFS=0
#offset in the discrete perp space (adjusts tiling vs decofile convention)

set MIRROR=1
set ROT2=0
set NROT=5
set DPZ="1 0"
# 5-fold rotation is automatic; the following are OPTIONAL:
# mirror  -> mirror parallel to e_1
# screw ->  2-fold screw (turns into 10-fold screw, because of the 5-fold sym)

set dpmodel=0

set perp=0 #available via option
set lift=" "
#perp-lift: [1/0] [PERP-BOUND] [TOL]
set redefined=0
set export_sym=1  # option rsym -> "recycle sym."

set DBNAME=
set FIG=0
set QUERY=0
set QUERYOBJ=0
set QOBJ=
set COLORS=
if (-e "INPUT/FIG/colors.def")then
    set COLORS="-c INPUT/FIG/colors.def"
endif
##################### really start here ##########################

if !(-e $SRCDIR) then
    echo "Directory $SRCDIR does not exist."
    echo "Probably env variable DECAHOME is not set (from .cshrc)."
    exit
endif
if !(-e $EXEC) then
    echo "Executable $EXEC does not exist."
    exit
endif
set DECO="decoration.inp"  # default
set myarg=()
foreach arg ($argv)
    if (-e $arg ) then
	set DECO=$arg
    else
	set myarg=($myarg $arg)
    endif
end
set argv=($myarg)
#### OPTIONS
set TEST=0
### options in DECOFILE are first, cmdline overrides
set argv_deco=()
if(-e $DECO)then
    set argv_deco=`perl -ne 'if(/^\s*?def\{\s*(.+)\}/){print "-",$1," "}' $DECO`
endif
#echo $argv_deco
#exit
set argv=($argv_deco $argv)
##MEMO  $* Equivalent  to  `$argv',  which  is  equivalent to `$argv[*]'. 
#echo DEBUG "$*"
while( "$*" != "" )
#    if ( $# == 0 ) then
#	goto skip_options
#    endif
# echo DEBUG2 $1
    switch ( "$1" )
	case -prag:
	    shift
	    if ( $#argv == 0 ) then
		echo "Pragma definitions for the decofile parser: "
		awk '$0~"test\\[1\\]=="{match($0,/".*"/);print "\\"substr($0,RSTART+1,RLENGTH-2)}' $DECAHOME/SRC/deco_parser.awk | sort
		exit
	    endif
	    $PRAGHELP $1
	    exit
	breaksw
	case -h:
	    goto helpmessage
	breaksw
	case -help:
	    helpmessage:
	    shift
	    if ( $#argv == 0 ) then
		$LSOPT
		exit
	    endif
	    $PHELP $1
	    exit
	breaksw
	case -lift:
	    shift
	    set lift=$1
	    shift
	    set lift="$lift "$1
	    set perp=1
	breaksw
	case -rsym: #sets export_sym to false; means the existing INPUT/symmetry.def will be reused; but first check if the file exists.
	    if(! -e INPUT/symmetry.def)then
		echo "Option -rsym inhibits generation of new INPUT/symmetry.def; but the file INPUT/symmetry.def does not exist." | fold -s -w 70
		exit
	    endif
	    set export_sym=0
	breaksw
	case -mirror:
#	    shift
	    set MIRROR=1
	breaksw
	case -dpscrew:
	    goto defscrew
	breaksw
	case -screw:
	    defscrew:
	    set ROT2=1
	    set DPZ="1 1"
	breaksw
	case -dpinv:
	    set ROT2=1
	    set DPZ="-1 0"
	breaksw
	case -dp2:
	    set ROT2=1
	    set DPZ="1 0"
	breaksw
	case -dpmodel:
	    shift
	    if ($1 == 0)then
		set dpmodel=0
		set NUOFFS=0
		set NROT=10
	    endif
	    if ($1 == 1)then
		set dpmodel=1
	    endif
	    if ($1 == 2)then
		set dpmodel=2
	    endif
	breaksw
	case -nrot:
	    shift
	    set NROT=$1
	breaksw
	case -fig:
	    set FIG=1
	breaksw
	case -db:
	    set DBNAME=INPUT/OBJDB/database
	    if(! -e $DBNAME)then
		echo "Default object database $DBNAME not found."
		exit
	    endif
	breaksw
	case -q:
	    set QUERY=1
	breaksw
	case -qq:
	    shift
	    set QUERYOBJ=1
	    set QOBJ=$1
	breaksw
	case -rdb:
	    shift
	    set DBNAME=$1
	    if(! -e $DBNAME)then
		echo "Object database $DBNAME not found."
		exit
	    endif
	breaksw
	case -nuoffs:
	    shift
	    set NUOFFS=$1
	breaksw
	case -test:
	    set TEST=1
	breaksw
	default:
	    echo "mkdeco : unknown option $1"
	    $LSOPT
	    exit
	breaksw
    endsw
    shift
end # options
if !( -e $DECO ) then
    echo "Undefined decoration file DECOFILE."
    echo "SYNOPSIS: mkdeco [-h [OPTION] | DECOFILE] [OPTIONS]"
    echo "Available options: " 
    awk '$1=="case"{print substr($2,1,length($2)-1)}' $DECAHOME/scripts/mkdeco | sort | awk '{printf $0" "}END{print ""}' | fold -s -w 70
    exit
endif
#now mode[1] is error code (0=OK), and mode[2] is 1 (atoms) or 2 (tile-ham)
if !(-d INPUT) then
    #check if $DECAHOME/INPUT exists
    if !(-d $DECAHOME/INPUT) then
	echo "INPUT dir does not exist, and cannot find DECAHOME/INPUT."
	exit
    else
	cp -r $DECAHOME/INPUT .
    endif
endif

# SYMMETRY STUFF -> INPUT/symmetry.def || skip if export_sym == 0
if ($export_sym == 1) then
    set SYMM=($NROT $MIRROR $ROT2)
#    echo "SYMM: $NROT $MIRROR $ROT2   $SYMM" > /dev/stderr
    #awk -f $SRCDIR/sym.awk $SYMM
    #exit
    set symerr=`awk -f $SRCDIR/sym.awk $SYMM`
    if($symerr == 1)then
	echo "Error in symmetry-elements definition. Allowed elements are"' "s" "m" or "2"'", now SYMM={$SYMM}. Check variable SYMM at the top of this file." | fold -s -w 60
	exit
    endif
endif

if(-f INPUT/MKDECO/bc.inp) cat INPUT/MKDECO/bc.inp > .bcinp
if ( $QUERY == 1 ) then
    awk -f $SRCDIR/deco_parser.awk $DECO $DBNAME $COLORS -q $dpmodel| sort +1 -b
    exit
endif
if ( $QUERYOBJ == 1 ) then
    awk -f $SRCDIR/deco_parser.awk $DECO $DBNAME $COLORS -qq $QOBJ $dpmodel
    exit
endif
# customary mkdeco.inp file:
set out="INPUT/mkdeco.inp"
echo $DECO > $out
echo "$FIG # 1=generate INPUT/objects.fig">> $out
echo $NUOFFS" # nuoffs: discrete perp-space offset" >> $out
echo $dpmodel" # dpmodel: coupling discrete-perp vs screw" >> $out
echo $DPZ"  # dpz: binding between 2-fold rotation and action on Z-coord" >> $out
echo "$perp $lift  # perp-lift: [1/0] [PERP-BOUND] [TOL]" >> $out
set mode=`awk -f $SRCDIR/deco_parser.awk $DECO $DBNAME $COLORS $dpmodel >>$out `
#echo $mode
#exit
set nli=`wc -l $out`
# launch! (if no error in $DECO)
if( $nli[1] > 6 )then
#echo LAUNCH
    $EXEC
endif

##

